
importPackage(com.blocks.blocks);
importPackage(com.blocks);
importPackage(com.blocks.model);
importPackage(com.jme3.math);
importPackage(java.lang);
importPackage(java.util);
var application = new Object();
var mapactual = [1];

var generations = 80;
var generation = 0;

var DELAY = 1;
var tpfCount = 0;
var lastMasterId = 0;
var levelValue = 0;
/**
 * Automaton
 * TODO change the order of the cell Variables so 111 is 8 and 000 is 0
 */
application.setup = function() {

	ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockX(
			false);
	ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockY(
			false);
	ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockZ(
			false);

	ScriptBlocksApplication.getInstance().getLevelProperties().setLevelDescription("Non Playable demo level of Cell Automaton")
	ScriptBlocksApplication.getInstance().getLevelProperties().setLevelFileName("levelAutomaton.js");
	ScriptBlocksApplication.getInstance().getLevelProperties().setNextLevelFileName("levelGOL.js");	

}

application.testValue = function(cellZero,cellOne,cellTwo) {
	return application.ruleNinty(cellZero,cellOne,cellTwo);
}
application.ruleOneEightyTwo = function(cellZero, cellOne, cellTwo){

	var zeroCell = [1,1,1,1];
	var oneCell = [1,1,0,0];
	var twoCell = [1,0,1,1];
	var threeCell = [1,0,0,1];
	var fourCell = [0,1,1,0];
	var fiveCell = [0,1,0,1];
	var sixCell = [0,0,1,1];
	var sevenCell = [0,0,0,0];
	
    return application.cellCheck(cellZero, cellOne, cellTwo, zeroCell,oneCell,twoCell,threeCell,fourCell,fiveCell,sixCell,sevenCell);
	
}
application.ruleNinty = function(cellZero, cellOne, cellTwo){

	var zeroCell = [1,1,1,0];
	var oneCell = [1,1,0,1];
	var twoCell = [1,0,1,0];
	var threeCell = [1,0,0,1];
	var fourCell = [0,1,1,1];
	var fiveCell = [0,1,0,1];
	var sixCell = [0,0,1,1];
	var sevenCell = [0,0,0,0];
	
    return application.cellCheck(cellZero, cellOne, cellTwo, zeroCell,oneCell,twoCell,threeCell,fourCell,fiveCell,sixCell,sevenCell);
	
}

application.ruleThirty = function(cellZero, cellOne, cellTwo){

	var zeroCell = [1,1,1,0];
	var oneCell = [1,1,0,0];
	var twoCell = [1,0,1,0];
	var threeCell = [1,0,0,1];
	var fourCell = [0,1,1,1];
	var fiveCell = [0,1,0,1];
	var sixCell = [0,0,1,1];
	var sevenCell = [0,0,0,0];
	
	return application.cellCheck(cellZero, cellOne, cellTwo, zeroCell,oneCell,twoCell,threeCell,fourCell,fiveCell,sixCell,sevenCell);
}

application.ruleOneHundredTen = function(cellZero, cellOne, cellTwo){

	var zeroCell = [1,1,1,0];
	var oneCell = [1,1,0,1];
	var twoCell = [1,0,1,1];
	var threeCell = [1,0,0,0];
	var fourCell = [0,1,1,1];
	var fiveCell = [0,1,0,1];
	var sixCell = [0,0,1,1];
	var sevenCell = [0,0,0,0];
	
	return application.cellCheck(cellZero, cellOne, cellTwo, zeroCell,oneCell,twoCell,threeCell,fourCell,fiveCell,sixCell,sevenCell);
}

application.ruleOneNinty = function(cellZero, cellOne, cellTwo){

	var zeroCell = [1,1,1,1];
	var oneCell = [1,1,0,0];
	var twoCell = [1,0,1,1];
	var threeCell = [1,0,0,1];
	var fourCell = [0,1,1,1];
	var fiveCell = [0,1,0,1];
	var sixCell = [0,0,1,1];
	var sevenCell = [0,0,0,0];
	
	return application.cellCheck(cellZero, cellOne, cellTwo, zeroCell,oneCell,twoCell,threeCell,fourCell,fiveCell,sixCell,sevenCell);
}
//cellZero, cellOne, cellTwo : the values to be checked
//zeroCell,oneCell,twoCell,threeCell,fourCell,fiveCell,sixCell,sevenCell : the rules values
application.cellCheck = function(cellZero, cellOne, cellTwo, zeroCell,oneCell,twoCell,threeCell,fourCell,fiveCell,sixCell,sevenCell){
	/**
	 * var zeroCell = [1,1,1,1];
	var oneCell = [1,1,0,0];
	var twoCell = [1,0,1,1];
	var threeCell = [1,0,0,1];
	var fourCell = [0,1,1,1];
	var fiveCell = [0,1,0,1];
	var sixCell = [0,0,1,1];
	var sevenCell = [0,0,0,0];
	 */
	
	if(cellZero == zeroCell[0] && cellOne == zeroCell[1] && cellTwo == zeroCell[2]){
		return zeroCell[3];
	}else if(cellZero == oneCell[0] && cellOne == oneCell[1] && cellTwo == oneCell[2]){
		return oneCell[3];
	}else if(cellZero == twoCell[0] && cellOne == twoCell[1] && cellTwo == twoCell[2]){
		return twoCell[3];
	}else if(cellZero == threeCell[0] && cellOne == threeCell[1] && cellTwo == threeCell[2]){
		return threeCell[3];
	}else if(cellZero == fourCell[0] && cellOne == fourCell[1] && cellTwo == fourCell[2]){
		return fourCell[3];
	}else if(cellZero == fiveCell[0] && cellOne == fiveCell[1] && cellTwo == fiveCell[2]){
		return fiveCell[3];
	}else if(cellZero == sixCell[0] && cellOne == sixCell[1] && cellTwo == sixCell[2]){
		return sixCell[3];
	}else if(cellZero == sevenCell[0] && cellOne == sevenCell[1] && cellTwo == sevenCell[2]){
		return sevenCell[3];
	}
}
application.loop = function(name, tpf) {
	if (tpfCount > DELAY) {
		application.testGeneration(++levelValue);
		tpfCount = 0;
	} else {
		tpfCount = tpfCount + tpf;
	}
}

application.testGeneration = function(level) {
	// copy array
	if(level==1){
		//println("mapactual" + mapactual);
		var tempamp = new Array();
		for(x=0; x<mapactual.length ; x++){
			tempamp[x]=mapactual[x];
		}
		for(x=0; x<tempamp.length ; x++){			
			var value = tempamp[x];
			if(value == 1){
				application.addNPC(new Vector3f( x*2, level*2,0.0),++lastMasterId);
			}
		}
		//println("mapactual" + mapactual);
	}else if(level <=generations){
		var tempamp = new Array();
		for(x=0; x<mapactual.length ; x++){
			tempamp[x]=mapactual[x];
		}
		//println("TempAMP" + tempamp);
		// Clear Array
		mapactual = new Array();
		mappointer=0;
		for(x=0; x<tempamp.length ; x++){
			var cellZero = 0;
			var cellOne = 0;
			var cellTwo = 0;
			if(x==0){
				// Left Pad
				//print("L");
				cellTwo = tempamp[x];				
				var value = application.testValue(cellZero,cellOne,cellTwo);
				if(value == 1){
					application.addNPC(new Vector3f( (x*2) - (level*2), level*2,0.0),++lastMasterId);
				}
				mapactual[mappointer++] = value;
			}	
			{
				//print("C"); 
				if(x>0){	
					 cellZero = tempamp[x-1];
				 }
				 cellOne = tempamp[x];
				 if(x<tempamp.length-1){
					 cellTwo = tempamp[x+1];
				 }
				var value = application.testValue(cellZero,cellOne,cellTwo);
				if(value == 1){
					application.addNPC(new Vector3f( (x*2) - (level*2), level*2,0.0),++lastMasterId);
				}
				mapactual[mappointer++] = value;
			
			} 
			if(x==tempamp.length-1){
				//println("R");
				// Right Pad
				cellZero = tempamp[x];				
				var value = application.testValue(cellZero,cellOne,cellTwo);
				if(value == 1){
					application.addNPC(new Vector3f( (x*2) - (level*2), level*2,0.0),++lastMasterId);
				}
				mapactual[mappointer++] = value;
			}	
			
		}
	}

}

application.addNPC = function(vector3f, masterId) {

	// var masterId = System.currentTimeMillis();
	var masterBlock = new MasterBlock("masterBlock", masterId);
	masterBlock.init();
	masterBlock.setLocalTranslation(vector3f);
	ScriptBlocksApplication.getInstance().getRootNode()
			.attachChild(masterBlock);

// var Block1992909979 = new Block("Block1992909979", masterId);
// Block1992909979.setBlockSides(BlockSides.SIDE_ABOVE);
// Block1992909979.setCustomFrequency(true);
// Block1992909979.setScriptName("/blocks/PlusOne.js");
// Block1992909979.init();
// Block1992909979.setLocalTranslation(new Vector3f(0.0, 2.0, 0.0));
// masterBlock.attachChild(Block1992909979);
// var Block1193736688 = new Block("Block1193736688", masterId);
// Block1193736688.setBlockSides(BlockSides.SIDE_ABOVE);
// Block1193736688.setCustomFrequency(true);
// Block1193736688.setScriptName("/blocks/TwoX.js");
// Block1193736688.init();
// Block1193736688.setLocalTranslation(new Vector3f(0.0, 2.0, 0.0));
// Block1992909979.attachChild(Block1193736688);
//	
// var DetachedBlock1909895466 = new DetachedBlock("DetachedBlock1909895466",
// masterId);
// DetachedBlock1909895466.setBlockSides(BlockSides.SIDE_ABOVE);
// DetachedBlock1909895466.init();
// DetachedBlock1909895466.setLocalTranslation(new Vector3f(0.0, 2.0, 0.0));
// Block1193736688.attachChild(DetachedBlock1909895466);

	masterBlock.updatePower(false);
	// masterBlock.updateMasterId(masterId);
	ScriptBlocksApplication.getInstance().addMasterBlock(
			masterBlock.getMasterId(), masterBlock);

	return masterBlock;

}
